package com.niit.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusic.common.R;
import com.niit.onlinemusic.mapper.AdminMapper;
import com.niit.onlinemusic.model.domain.Admin;
import com.niit.onlinemusic.model.request.AdminRequest;
import com.niit.onlinemusic.service.AdminService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * 管理员的业务实现
 *
 * @author zuwei522
 * @since 2025/9/16 16:06
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    private final AdminMapper adminMapper;

    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    @Override
    public R verifyPassword(AdminRequest adminRequest, HttpSession session) {
        String username = adminRequest.getUsername();
        // 使用 MyBatis Plus 查询完整数据
        // 设置查询条件
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        queryWrapper.eq("password", adminRequest.getPassword());
        // 查询数据库是否有匹配的用户名与密码
        if (adminMapper.selectCount(queryWrapper) > 0) {
            session.setAttribute("name", username);
            return R.success("登录成功");
        } else {
            return R.error("用户名或密码错误");
        }
    }
}
